var api = require('../../../config/api.js');
var util = require('../../../utils/util');
var app = getApp();

Page({
    data: {
        activeIndex: 0,
        years: [],
        year: '',
        month: '',
        qrcodeImage: '',
        user: {},
        page:1,
        yearsIndex1: 0,
        yearsIndex2: 0,
        goodsList: [
        ],
        echoVal: [], //当前日期回显
        teamList: [
        ],
        navItems: [
            { title: '我的团队', index: 0, isActive: true },
            { title: '订单明细', index: 1, isActive: false },
            { title: '邀请', index: 2, isActive: false },
        ],
        titleHeight: 0, //状态栏和导航栏的总高度
        statusBarHeight: 0, //状态栏高度
        naviBarHeight: 0, //导航栏高度
        userInfo: {},
        navHeight:0
    },
    onLoad() {
        const res = wx.getSystemInfoSync()
        const system = res.platform
        const userInfo = wx.getStorageSync('userInfo')
        let menuButtonObject = wx.getMenuButtonBoundingClientRect();
        this.setData({
            statusBarHeight: res.statusBarHeight,
            userInfo: userInfo
        })
        if (system === 'android') {
            this.setData({
                titleHeight: (48 + this.data.statusBarHeight)
            })
        } else if (system === 'ios') {
            this.setData({
                titleHeight: (44 + this.data.statusBarHeight)
            })
        }
        wx.getSystemInfo({
            success: res => {
              // 导航高度
              let statusBarHeight = res.statusBarHeight,
              navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
              this.setData({
                navHeight
            })
            }
          })
        this.setData({
            naviBarHeight: this.data.titleHeight - this.data.statusBarHeight,
        })
        var date = new Date();
        var year = date.getFullYear(); //获取完整的年份(4位)
        var month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        this.setData({
            yearsIndex1: year - 1500,
            yearsIndex2: month - 1
        });
        let numbers = [];
        for (let i = 1500; i <= 2999; i++) {
            numbers.push(i);
        }
        var arr = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
        this.setData({
            years: [numbers, arr],
            echoVal: [year - 1500, month - 1],
            year,
            month
        });
        // this.temaApi(userInfo)

        wx.showShareMenu({
            withShareTicket: true, // 是否使用带 shareTicket 的转发详情
            menus: ['shareAppMessage'], // 要显示的分享菜单列表，这里只包括分享给好友的选项
            success: function () {
                console.log('分享菜单显示成功');
            },
            fail: function (error) {
                console.error('分享菜单显示失败:', error);
            }
        });
    },
    onShow(){
        this.temaApi(this.data.userInfo)
    },
    getMax(timestamp) {
        console.log(timestamp)
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();
        const second = date.getSeconds();
        const formattedMonth = this.padZero(month);
        const formattedDay = this.padZero(day);
        const formattedHour = this.padZero(hour);
        const formattedMinute = this.padZero(minute);
        const formattedSecond = this.padZero(second);
        return `${year}-${formattedMonth}-${formattedDay} ${formattedHour}:${formattedMinute}:${formattedSecond}`;
      },
      // 在个位数前补零
       padZero(num) {
        return num < 10 ? '0' + num : num;
      },
    onShareAppMessage: function () {
        let that = this;
        let userInfo = wx.getStorageSync('userInfo') || {}
        return {
            desc: '唯爱与美食不可辜负',
            path: '/pages/index/index?parentUid=' + userInfo.userId
        }
    },
    temaApi(userInfo) {
        let that = this
        // 我的团队
        util.request(api.marketingTeamUser, { userId: userInfo.userId }, 'get').then(res => {
            if (res.errno === 0) {
                this.setData({
                    user: res.data
                })
            }
        })
        util.request(api.marketingTeamList, {
            userId: userInfo.userId
        }, 'get').then(function (res) {
            if (res.errno === 0) {
                if(res.data){
                    res.data.map((r)=>{
                        r.addTime = that.getMax(r.addTime)
                    })
                    that.setData({
                        teamList: res.data
                    })
                }
            }
        })
    },
    // 提现
    onWit(){
        const money = this.data.user
        const userId = this.data.userInfo.userId
        wx.navigateTo({
          url: '/pages/ucenter/withdrawal/withdrawal?money='+money.price + '&userId='+userId, 
        })
    },
    yearChange: function (e) {
        console.log(e)
        //获得对象的 detail的 value
        //通过数组的下标改变显示在页面的值
        this.yearsIndex1 = e.detail.value[0];
        this.yearsIndex2 = e.detail.value[1];
    },
    delat() {
        wx.navigateBack({
            delta: 1,
        })
    },
    handlePickerChange: function (event) {
        const { value } = event.detail;
        const yearIndex = value[0];
        const monthIndex = value[1];
        this.setData({
            year: this.data.years[0][yearIndex],
            month: this.data.years[1][monthIndex],
            page:1,
            goodsList:[]
        });
        this.detalisSi()
    },
    dadada(e){
        if(this.data.activeIndex === 1){
            this.setData({
                page:this.data.page+1
            })
            this.detalisSi()
        }
    },
    detalisSi() {
        let that = this;
        let userInfo = that.data.userInfo;
        // 订单明细
        let a = that.data.month.toString().padStart(2, '0')
        let date = that.data.year + '-' + a
        util.request(api.marketingTeam, {
            userId: userInfo.userId,
            page: that.data.page,
            limit: 14,
            time: date
        }, 'get').then(function (res) {
            if (res.errno === 0) {
                if(res.data){
                    that.setData({
                        goodsList: that.data.goodsList.concat(res.data)
                    })
                }
            }
        })
    },
    handleNavClick(e) {
        let index = e.currentTarget.dataset.index
        let userInfo = this.data.userInfo;
        let that = this;
        switch (index) {
            case 0:
                that.temaApi(userInfo)
                break;
            case 1:
                that.detalisSi()
                break;
            case 2:
                break;
            default:
                break;
        }
        this.setData({
            activeIndex: index,
            page:1,
            goodsList:[]
        })

    }
});
